package com.gdlgxy.oa.controller;

import com.gdlgxy.oa.DAO.KindListDao;
import com.gdlgxy.oa.bean.Fare;
import com.gdlgxy.oa.bean.FareList;
import com.gdlgxy.oa.service.FareServer;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.TreeMap;


public class FareUpdateCtrl extends HttpServlet {

    /**
     * 修改的支出链表
     */
    private FareList list;
    /**
     * 要修改的支出节点
     */
    private Fare temp;


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取动态应用的根路径
        String contextPath = request.getContextPath();

        // 设置响应的内容类型，防止中文乱码问题
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        out.print(" <!DOCTYPE html>");
        out.print(" <html lang='zh-CN'>");
        out.print(" <head>");
        out.print("     <meta charset='UTF-8'>");
        out.print("     <meta http-equiv='X-UA-Compatible' content='IE=edge'>");
        out.print("     <meta name='viewport' content='width=device-width, initial-scale=1.0'>");
        out.print("     <title>修改支出</title>");
        out.print("     <style>");
        out.print("                 h1 {");
        out.print("             padding: 0px 20px;");
        out.print("             border-left: 10px solid #ed515191;");
        out.print("             background-color: rgba(208, 208, 208, 0);");
        out.print("             font-size: 19px;");
        out.print("             line-height: 30px;");
        out.print("             color: cornflowerblue;");
        out.print("             font-weight: bold;");
        out.print("             margin: 5px 0;");
        out.print("         }");
        out.print("         form {");
        out.print("             display: block;");
        out.print("             width: 250px;");
        out.print("             margin: auto;");
        out.print("         }");
        out.print("         li {");
        out.print("             list-style: none;");
        out.print("         }");
        out.print("         ul.IsignUp li {");
        out.print("             margin-top: 5px;");
        out.print("         }");
        out.print("         input.inputLine {");
        out.print("             width: 226px;");
        out.print("             padding: .375rem .75rem;");
        out.print("             border-radius: .25rem;");
        out.print("             border: 1px solid #ced4da;");
        out.print("         }");
        out.print("         input.inputLine:focus {");
        out.print("             border-style: solid;");
        out.print("             border-color: #03a9f4;");
        out.print("             box-shadow: 0 0 5px #03a9f4;");
        out.print("         }");
        out.print("         input.inputLeft {");
        out.print("             width: 130px;");
        out.print("         }");
        out.print("         button,");
        out.print("                 select {");
        out.print("             padding: .375rem .75rem;");
        out.print("             border-radius: .25rem;");
        out.print("             border: 1px solid #ced4da;");
        out.print("         }");
        out.print("         button:hover,");
        out.print("                 select:hover {");
        out.print("             border-color: #03a9f4;");
        out.print("         }");
        out.print("         button:active {");
        out.print("             background: #03a9f4;");
        out.print("         }");
        out.print("     </style>");
        out.print(" <body>");
        out.print("     <h1>修改支出界面</h1>");
        // 以上是固定的
        // 获取学生编号
        int sno = Integer.parseInt(request.getParameter("Sno"));
        int fno = Integer.parseInt(request.getParameter("Fno"));

        out.print("     <form action='"+contextPath+"/fare/update' method='post'>");
        out.print("         <ul class='IsignUp' style='padding:0px'>");

        // 根据学生编号查询信息
        list = FareServer.list(sno);
        temp = list.fareFind(fno);

        out.print("             <li> 支出金额： <input class='inputLeft inputLine' type='text' id='name' name='Fmoney' value='"+temp.getMoney()+"'>");
        out.print("                 支出类型： <select class='selectRight inputLine' name='Kno' id='hobby' >");

        // 获取数据库中的支出种类
        TreeMap<Integer, String> map = KindListDao.getKindList();

        for (Map.Entry<Integer, String> entry : map.entrySet()) {
            if (entry.getKey() != temp.getKno()) {
                out.print("              <option value='"+entry.getKey()+"'>"+entry.getValue()+"</option>");
            }else{
                out.print("              <option selected='selected' value='"+entry.getKey()+"'>"+entry.getValue()+"</option>");
            }
        }
        out.print("                 </select> </li>");
        out.print("             <li> <input class='inputLine' style='height:80px;' id='remark' name='Fremark' value='"+temp.getRemark()+"' />");

        // 以下是固定的
        out.print("             </li>");
        out.print("             <li> <button class='inputLine' type='submit' class='i-button' onclick='addOneToOne()'> 确认修改 </button> </li>");
        out.print("         </ul>");
        out.print("     </form>");
        out.print(" </body>");
        out.print(" </html>");

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 注意乱码问题
        request.setCharacterEncoding("UTF-8");


        // 修改的金额判断
        String buffer = request.getParameter("Fmoney");
        double fmoney = 0;
        if (!buffer.isEmpty()) {
            // 用户输入为金钱
            fmoney = Double.parseDouble(buffer);
            temp.setMoney(fmoney);
        }else{
            // 重新生成金钱
            temp.randomMoney();
        }

        int kno = Integer.parseInt(request.getParameter("Kno"));
        String fremark = request.getParameter("Fremark");


        temp.setKno(kno);
        temp.setRemark(fremark);
        System.out.println("controller：修改的支出对应的学生编号为："+list.getSno());
        System.out.println("controller：修改的支出编号为："+temp.getFno());
        System.out.println("controller：修改的支出金额为："+fmoney);
        System.out.println("controller：修改的支出类型号为："+kno);
        System.out.println("controller：修改的支出备注为："+fremark);

        // 向server层传递修改后的单向链表
        // 向数据库传递修改后链表
        // 修改成功就转发回支出链表
        if (FareServer.update(list)) {
            request.getRequestDispatcher("/fare/list?Sno=" + list.getSno()).forward(request, response);
        } else {
            // 修改失败
            request.getRequestDispatcher("/error.html").forward(request, response);
        }

    }
}
